from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.contrib.auth.hashers import make_password, check_password


from TestModel.models import User

def hello(request):
    return HttpResponse("Hello world ! ")

def login_index(request):
    """
    登陆界面
    """
    if request.method == "POST":
        username = request.POST.get('username')
        password = request.POST.get('pwd')
        try:
            user_info = User.objects.get(username=username)
        except Exception as e:
            return render(request, 'login.html', {'error': '用户名错误，请重试。'})
        if check_password(password, user_info.password):
            request.session['username'] = username
            if user_info.type_user == "1":
                return redirect("/index/")
            else:
                return redirect("/super_index/")
        else:
            return render(request, 'login.html', {'error': '密码错误，请重试。'})
        # if username == "admin" and password == "123":
        #     return redirect('/main/')
        # else:
        #     return render(request, './html/login.html', {'error': '用户名或密码错误，请重试。'})

    return render(request, "login.html")

def register(request):
    """
    注册界面
    """
    if request.method == "POST":
        print("进入这里了")
        try:
            name = request.POST.get('name')
            username = request.POST.get('username')
            password = request.POST.get('pwd')
            # 数据加密
            password = make_password(password)
            print(password)
            # 账号密码验证

            user = User(name=name, username=username, password=password, type_user=1)
            # 存到数据库中
            user.save()
            return render(request, "register.html", {'context': "创建成功"})
        except Exception as e:
            print(e)
            return render(request, "register.html", {"error": e})
    return render(request, "register.html")

def super_index(request):
    """
    super_index功能选择界面
    """
    username = request.session.get('username', None)
    user_info = User.objects.filter(username=username).first()
    return render(request, "super_index.html", {'name': user_info.name})

def index(request):
    """
    index功能选择界面
    """
    username = request.session.get('username', None)
    user_info = User.objects.filter(username=username).first()
    return render(request, "index.html", {'name': user_info.name})

def main(request):
    """
    主要功能界面
    """
    # 用session做校验
    if request.session.get('username', None) != None:
        return render(request, "main.html")
    else:
        return render(request, 'login.html')

def welcome(request):
    """
    欢迎界面
    """
    # 用session做校验
    if request.session.get('username', None) != None:
        return render(request, "welcome.html")
    else:
        return render(request, 'login.html')

def user(request):
    """
    用户管理界面
    """
    # 用session做校验
    if request.session.get('username', None) != None:
        username = request.session.get('username', None)
        if request.method == "POST":
            name = request.POST.get('name')
            email = request.POST.get('email')
            phone = request.POST.get('phone')
            user_info = User.objects.get(username=username)
            user_info.name = name
            user_info.email = email
            user_info.phone = phone
            # 更新用户信息
            user_info.save()
            return render(request, "user.html", {'context': "保存成功"})

        return render(request, "user.html", {'username': username})
    else:
        return render(request, 'login.html')

def change_pwd(request):
    """
    修改密码
    """
    # 用session做校验
    if request.session.get('username', None) != None:
        if request.method == 'POST':
            username = request.session.get('username', None)
            user_info = User.objects.get(username=username)
            old_password = request.POST['old_password']
            new_password1 = request.POST['new_password1']
            new_password2 = request.POST['new_password2']
            print(check_password(old_password, user_info.password))
            if not check_password(old_password, user_info.password):
                return render(request, 'change_pwd.html', {"error": "原密码错误", "context": ""})
            elif new_password1 != new_password2:
                return render(request, 'change_pwd.html', {"error": "两次输入的新密码不一致", "context": ""})
            else:
                # 加密
                new_password1 = make_password(new_password1)
                # 修改密码
                user_info.password = new_password1
                user_info.save()

                return render(request, 'change_pwd.html', {"context": "密码修改成功", "error": ""})


        return render(request, 'change_pwd.html')
    else:
        return render(request, 'login.html')

def user_manage(request):
    """
    管理员用户管理
    """
    # 用session做校验
    if request.session.get('username', None) != None:
        users = User.objects.all()
        if request.method == "POST":
            username = request.session.get('username', None)
            user_info = User.objects.get(username=username)
            updated_data = {
                # 'username': request.POST.get('username'),
                'name': request.POST.get('name'),
                'password': request.POST.get('pwd'),
                'phone': request.POST.get('phone') if judge(request.POST.get('phone')) else None,
                'email': request.POST.get('email') if judge(request.POST.get('email')) else None,
                'type_user': request.POST.get('type_user'),
            }
            User.objects.filter(username=username).update(**updated_data)

            return render(request, 'user_manage.html', {'users': users, 'context': "修改成功"})
        return render(request, 'user_manage.html', {'users': users})
    else:
        return render(request, 'login.html')

def judge(data):
    if data == "None":
        return False
    else:
        return True



# def judge(request):
